<template>
  <div>
    <div class="dialogPop">
      <el-dialog :visible.sync="addTeaData.dialogAddTea" :before-close="handleClose">
        <p class="dialog-title">添加教师</p>
        <el-form :model="addTeaPop" :rules="rulesAddTeaPop" ref="addTeaPop" label-width="108px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学校名称" prop="schoolId">
                <el-select v-model="addTeaPop.schoolId" filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethodSchool" :loading="schoolLoading" style="width:100%">
                  <el-option v-for="(item,index) in formInline.schoolNameList" :key="index" :label="item.schoolName" :value="item.schoolId">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名：" prop="name">
                <el-input v-model="addTeaPop.name" placeholder="请输入姓名"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="注册手机号：" prop="phone">
                <el-input v-model="addTeaPop.phone" placeholder="请输入注册手机号" :maxlength="11"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别：">
                <el-radio-group v-model="addTeaPop.sex">
                  <el-radio label="0">男</el-radio>
                  <el-radio label="1">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button type="primary" @click="addTeacher('addTeaPop')">确 认</el-button>
            <el-button @click="resetFormdelDialogAddTea('addTeaPop')">取 消</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addTeaInfor, querySchoolByName } from 'api/teacherManage';
export default {
    props: {
        addTeaData: {
            default() {
                return {
                    dialogAddTea: false // 默认不显示
                }
            },
            required: true
        }
    },
    data() {
        return {
      // 添加学校弹框
      // dialogAddTea: false,
            schoolLoading: false, // 模糊查找学校加载提示
            formInline: {
                schoolNameList: [] // 学校名称列表
            },
            addTeaPop: {
                schoolId: '',
                name: '',
                phone: '',
                sex: '0'
            },
            rulesAddTeaPop: {
                schoolId: [{ required: true, message: '请输入学校名称', trigger: 'blur' }],
                name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                phone: [{ pattern: /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/, message: '请输入正确的联系方式', trigger: 'blur' }]
            }
        }
    },
    methods: {
    // 添加教师信息 确定按钮
        addTeacher(addTeaPop) {
            this.$refs[addTeaPop].validate((valid) => {
                if (valid) {// 验证通过
                    let creatorId = '1214325';
                    let schoolId = this.addTeaPop.schoolId;
                    let name = this.addTeaPop.name;
                    let phone = this.addTeaPop.phone;
                    let sex = this.addTeaPop.sex;
                    let schoolName = '';
                    this.formInline.schoolNameList.forEach(s => {
                        if (s.schoolId == schoolId) {
                            schoolName = s.schoolName;
                        }
                    });
                    //console.log(creatorId, schoolId, name, phone, sex, schoolName);
                    addTeaInfor(creatorId, schoolId, name, phone, sex, schoolName).then(data => {
                        this.$message({
                            type: 'success',
                            message: '添加教师成功！'
                        });
                        // this.searchFetchData();
                        this.$emit('search-list');// 更新列表
                        this.handleClose();
                    })
                } else {
                    //console.log('error submit!!');
                    return false;
                }
            });
        },

    // 添加教师弹出框的关闭按钮
        handleClose() {
            this.delDialogAddTea();
        },
    // 取消按钮 
        resetFormdelDialogAddTea(dataForm) {
            this.$refs[dataForm].resetFields();
            this.delDialogAddTea();
        },
    // 还原弹窗信息
        delDialogAddTea() {
            this.addTeaPop.schoolName = '';
            this.addTeaPop.name = '';
            this.addTeaPop.phone = '';
            this.addTeaPop.sex = '0';
            this.addTeaData.dialogAddTea = false;
        },
    // 模糊查找学校
        remoteMethodSchool(query) {
            if (query !== '') {
                setTimeout(() => {
                    this.schoolLoading = false;
                    querySchoolByName(query).then(response => {
                        this.formInline.schoolNameList = response.data.content;
                    });
                }, 100);
            } else {
                this.addTeaPop.schoolId = '';
            }
        }
    }


}
</script>
<style lang="scss" scoped>
@import '../style/dialog.scss';
</style>


